package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.web.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("/login")
    @RequireLogin
    @ResponseBody
    public Object get(Long id) {
        return userInfoService.get(id);
    }

    /**
     * 判断当前手机号有没有注册
     *
     * @param phone
     * @return 不存在那么就跳到注册页面
     */
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone) {
        AssertUtil.checkPhone(phone);
        Boolean checkPhone = userInfoService.checkPhone(phone);
        return !checkPhone;
    }

    /**
     * 根据手机号发送验证码
     *
     * @param phone
     * @return
     */
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.success();
    }

    /**
     * 用户的注册
     *
     * @param phone
     * @param nickname
     * @param password
     * @param rpassword
     * @param verifyCode
     * @return
     */
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return AjaxResult.success();
    }

    /**
     * 用户的登录
     */
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String username, String password, HttpServletResponse response) {
        String token = userInfoService.userLogin(username, password);
        CookieUtil.addCookie("token",token,response, RedisKeys.USER_LOGIN.getTime().intValue());
        /*Cookie c = new Cookie("token",token);
        c.setMaxAge(Consts.USER_INFO_TOKEN_VAI_TIME);
        c.setPath("/");
        response.addCookie(c);*/
        return AjaxResult.success();
    }
}
